package com.hoow.shakoow.shake

import android.annotation.SuppressLint
import android.os.*
import android.view.View
import android.view.View.OnClickListener
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.hoow.shakoow.ai.data.ReqAiBase
import com.hoow.shakoow.ai.data.RevObjShakeUserInfo
import com.hoow.shakoow.ai.data.RevObjUserAblum
import com.hoow.shakoow.ai.home.NetworkViewModel
import com.hoow.shakoow.base.BaseActivity
import com.hoow.shakoow.databinding.ActivityUserDetailBinding
import com.hoow.shakoow.enums.PageTagEnum
import com.hoow.shakoow.extend.*
import com.hoow.shakoow.shake.components.imageView.ShakeImageGallary
import com.bumptech.glide.Glide
import com.hoow.shakoow.utils.analytics.util.AnalyticsUtil


/** 用户详情页 */
@SuppressLint("CustomSplashScreen")
class UserDetailActivity : BaseActivity(), OnClickListener {

    //布局binding
    private lateinit var mActivityBinding: ActivityUserDetailBinding

    // 用户头像
    private var mUserAvatar: String? = null

    //api接口viewModel
    private val mNetworkViewModel by lazy {
        ViewModelProvider(this)[NetworkViewModel::class.java]
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mNetworkViewModel)
        return list
    }

    override fun onBindingViewTag(): String {
        return PageTagEnum.user_detail.name.uppercase()
    }

    override fun onBindingContentView(): View {
        mActivityBinding = ActivityUserDetailBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    @SuppressLint("SetTextI18n", "SourceLockedOrientationActivity")
    override fun onContentViewBinded() {
        try {
            //获取用户详情回调
            mNetworkViewModel.onGetUserInfoByUserIdLiveData.observe(this) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { result ->
                        updateUserDetail(result)
                    }
                }
            }

            // 点击头像
            mActivityBinding.imageUserAvatarValue.setOnClickListener {
                mUserAvatar?.let { imgUrl ->
                    if (imgUrl.isNotBlank()) {
                        val list = mutableListOf<RevObjUserAblum>()
                        list.add(RevObjUserAblum(id = "0", imgUrl = imgUrl))
                        ShakeImageGallary.showDialog(
                            fragmentManager = supportFragmentManager,
                            dataList = list,
                            index = 0
                        )
                    }
                }
            }

            // 点击相册
            mActivityBinding.llUserAblumBg.setOnClickListener(this)
            mActivityBinding.imageUserAlbumExt.setOnClickListener(this)
            mActivityBinding.imageUserAlbum1.setOnClickListener(this)
            mActivityBinding.imageUserAlbum2.setOnClickListener(this)
            mActivityBinding.imageUserAlbum3.setOnClickListener(this)

            // 打招呼
            mActivityBinding.textSayHello.setOnClickListener(this)

            // 当前用户
            val userId = intent.getStringExtra("userId") ?: ""
            // 获取用户详情
            val reqAiBase = ReqAiBase()
            reqAiBase.userId = userId
            mNetworkViewModel.getUserInfoByUserId(reqAiBase)

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            // 点击相册
            mActivityBinding.llUserAblumBg -> {
                startToUserAblumPage()

                // 打点：摇一摇
                AnalyticsUtil.saveHitPointInfo(
                    type = "USER_AVATAR_CLICK",
                    remark = ""
                )
            }
            mActivityBinding.imageUserAlbumExt -> {
                startToUserAblumPage()
                // 打点：摇一摇
                AnalyticsUtil.saveHitPointInfo(
                    type = "USER_AVATAR_CLICK",
                    remark = ""
                )
            }
            mActivityBinding.imageUserAlbum1 -> {
                startToUserAblumPage()
                // 打点：摇一摇
                AnalyticsUtil.saveHitPointInfo(
                    type = "USER_AVATAR_CLICK",
                    remark = ""
                )
            }
            mActivityBinding.imageUserAlbum2 -> {
                startToUserAblumPage()
                // 打点：摇一摇
                AnalyticsUtil.saveHitPointInfo(
                    type = "USER_AVATAR_CLICK",
                    remark = ""
                )
            }
            mActivityBinding.imageUserAlbum3 -> {
                startToUserAblumPage()
                // 打点：摇一摇
                AnalyticsUtil.saveHitPointInfo(
                    type = "USER_AVATAR_CLICK",
                    remark = ""
                )
            }
            // 打招呼
            mActivityBinding.textSayHello -> {
                val userId = intent.getStringExtra("userId") ?: ""
                val shakeId = intent.getStringExtra("shakeId") ?: ""
                val params = HashMap<String, String>()
                params["userId"] = userId
                params["shakeId"] = shakeId

                println(params)
                startToGreetPage(params)

                // 打点：摇一摇
                AnalyticsUtil.saveHitPointInfo(
                    type = "USER_TO_GREE_CLICK",
                    remark = ""
                )
            }
        }
    }

    /** 更新用户信息 */
    private fun updateUserDetail(result: RevObjShakeUserInfo) {
        // 设置缓存
        ShakeUserManager.mCurShakeUserInfo = result

        mUserAvatar = result.headImg
        result.headImg?.let { url ->
            if (url.isValidLink()) {
                Glide.with(mActivityBinding.imageUserAvatarValue)
                    .load(url)
                    .into(mActivityBinding.imageUserAvatarValue)
            }
        }

        // 设置昵称
        mActivityBinding.textUserNickNameValue.text = result.name

        // 设置性别
        mActivityBinding.textUserGenderValue.text = result.getGenderDisplay()

        // 设置相册
        result.userPhotos?.let { photos ->
            if (photos.isNotEmpty()) {
                val imgUrl = photos[0].imgUrl ?: ""
                if (imgUrl.isValidLink()) {
                    Glide.with(mActivityBinding.imageUserAlbum1)
                        .load(imgUrl)
                        .into(mActivityBinding.imageUserAlbum1)
                }
            }
            if (photos.size > 1) {
                val imgUrl = photos[1].imgUrl ?: ""
                if (imgUrl.isValidLink()) {
                    Glide.with(mActivityBinding.imageUserAlbum2)
                        .load(imgUrl)
                        .into(mActivityBinding.imageUserAlbum2)
                }
            }
            if (photos.size > 2) {
                val imgUrl = photos[2].imgUrl ?: ""
                if (imgUrl.isValidLink()) {
                    Glide.with(mActivityBinding.imageUserAlbum3)
                        .load(imgUrl)
                        .into(mActivityBinding.imageUserAlbum3)
                }
            }
        }

        // 设置个性签名
        mActivityBinding.textUserSignatureValue.text = result.description
    }
}